/*
 * @Descripttion: 
 * @Author: junhuafly
 * @Date: 2020-03-26 15:58:10
 * @LastEditors: junhuafly
 * @Email: junhuafly@163.com
 * @LastEditTime: 2020-04-20 13:57:18
 */
// pages/mine/setting/index.js

import { userAPI } from "../../../api/index";
import {
  returnStatus,
  returnStatusToast,
} from './../../../utils/index'

var appInst = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    advice: '',
    showPopup: false,
    currentSize: 0,
    aboutUrl: '',
    blocks:[],
    showBlock:false,
    navbarData:{
      showCapsule: 1,
      back: true
    }
  },
  onBack(){
    const {showPopup,showBlock} = this.data
    if(!showPopup && !showBlock){
      wx.navigateBack()
    }
    this.setData({
      showPopup:false,
      showBlock: false,
      'navbarData.title':'设置'
    })
  },
  onCancel({currentTarget:{dataset:{type}}}){
    this.setData({
      [type]:false
    })
  },
  onClearCache() {
    const _this = this
    wx.clearStorage({
      success() {
        wx.showToast({
          title: '清除成功',
          icon: 'success',
          duration: 800,
        });
        setTimeout(() => {
          _this.initCache()
        }, 800);
      }
    })
  },
  initCache() {
    // 获取缓存大小
    const _this = this
    wx.getStorageInfo({
      success({currentSize:c_size}) {
        _this.setData({
          currentSize:Number(c_size/1024).toFixed(1)
        })
      },
      fail() {
        wx.showToast({
          title: '调用系统方法出错',
          icon: 'none',
          image: '',
          duration: 800,
        });
      }
    })
  },
  showAdvice(){
    this.setData({
      showPopup:true,
      'navbarData.title':'投诉建议'
    })
  },
  async initBlock(){
    this.setData({
      blocks:[],
    })
    const res = await userAPI.queryUserBlock()
    const blocks = await returnStatus(res)
    return blocks
  },
  async onRemoveBlock({currentTarget:{dataset:{info:{customerId:userId}}}}){
    const res = await userAPI.removeBlock({userId})
    returnStatusToast(res,()=>{
      this.initBlock()
    })
  },
  async openBlock(){
    const blocks = await this.initBlock()
    this.setData({
      blocks,
      showBlock: true,
      'navbarData.title':'黑名单'
    })
  },

  // 用户反馈
  async onSubmit(){
    const res = await userAPI.handleFeedBack({
      content:this.data.content,
      noteId:0
    })
    returnStatusToast(res,()=>{
      wx.navigateTo({
        url: '/pages/mine/order/afterOrder/status/index?status=1&title=反馈成功&message=我们已收到您的反馈信息',
      })
    })
  },
  hideAdvice(){
    this.setData({
      showPopup: false
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function ({
    title
  }) {
    appInst.setTitle(title)
    this.initCache()
    this.setData({
      aboutUrl: `/pages/webView/index?src=http://app.davhaowu.com/wechat/about&version=${appInst.version}`
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})